package at.jku.se.fastsell.db4o;

import at.jku.se.fastsell.data.User;

import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.config.EmbeddedConfiguration;
import com.db4o.constraints.UniqueFieldValueConstraint;

/**
 * Creates and provides main database object
 */
public class Database {
	
	private static ObjectContainer db;
	
	private static final String FILE = "database.db4o";
	
	// ----------------------------------------------------------------------------------
	
	static {
		EmbeddedConfiguration config = Db4oEmbedded.newConfiguration();
		
		config.common().objectClass(User.class).objectField("name").indexed(true);
		config.common().add(new UniqueFieldValueConstraint(User.class, "name"));
		
		//new File(FILE).delete();
		
		db = Db4oEmbedded.openFile(config, FILE);
	}
	
	// ----------------------------------------------------------------------------------
	
	public static ObjectContainer getDb() {
		return db;
	}
	
}
